<?php

class ZFDulcet_Form extends ZFKiller_Form {

	const FORM_TYPE_CAD = 'cad';
	const FORM_TYPE_EDIT = 'edit';
	const FORM_TYPE_READONLY = 'readonly';

	private $_formName;
	private $_colorPickerInitied = false;
	protected $_typeForm;

	public function __construct($formName, $options = null) {
		if (!isset($options['class'])) {
			$options['class'] = 'formee';
		}
		if (!isset($options['id'])) {
			$options['id'] = 'form_' . preg_replace('#[^a-z0-9]#i', '', $formName);
		}
		if (!isset($options['typeForm'])) {
			$options['typeForm'] = self::FORM_TYPE_CAD;
		}

		parent::__construct($options);

		$this
				->addElementPrefixPath('ZFDulcet_Form_Decorator', PATH_ZFEXTEND . '/ZFDulcet/Form/Decorator', Zend_Form::DECORATOR)
				->addDisplayGroupPrefixPath('ZFDulcet_Form_Decorator', PATH_ZFEXTEND . '/ZFDulcet/Form/Decorator')
				->setMethod(Zend_Form::METHOD_POST)
				->removeDecorator('HtmlTag')
		;
		$this->setTypeForm($options['typeForm']);

		$this->_formName = $formName;
	}

	public function addDisplayGroupFooter(array $elements, $options = null) {
		foreach ($elements as $elementName) {
			$this->getElement($elementName)->removeDecorator('DtDdWrapper');
		}

		if (isset($options['name'])) {
			$name = $options['name'];
			unset($options['name']);
		}
		else {
			$name = 'footer-group';
		}

		$this->addDisplayGroup($elements, $name, $options);
		$dg = $this->getDisplayGroup($name);
		$dg->removeDecorator('Fieldset');
		$dg->removeDecorator('DtDdWrapper');
		$dg->removeDecorator('HtmlTag');
		$dg->addDecorator('HtmlTagGroupFooter', array(
			'tag' => 'section',
			'class' => 'box_footer',
		));
	}

	public function addDisplayGroupIn(array $elements, $options = null) {
		if (isset($options['name'])) {
			$name = $options['name'];
			unset($options['name']);
		}
		else {
			$name = 'in-group';
		}

		$this->addDisplayGroup($elements, $name, $options);
		$dg = $this->getDisplayGroup($name);
		//$dg->removeDecorator('Fieldset');
		$dg->removeDecorator('DtDdWrapper');
		$dg->addDecorator('HtmlTag', array(
			'tag' => 'div',
			'class' => 'in',
		));
	}

	public function addElement($element, $name = null, $options = null) {
		if ($element instanceof ZFDulcet_Form_Element_Colorpicker) {
			if (!$this->_colorPickerInitied) {
				Zend_Layout::getMvcInstance()->getView()->headLink()->prependStylesheet(ZFDULCET_LIB_DIR_PLUGINS . '/colorpicker/css/colorpicker.css');
				Zend_Layout::getMvcInstance()->getView()->headScript()->appendFile(ZFDULCET_LIB_DIR_PLUGINS . '/colorpicker/js/colorpicker.js');
				Zend_Layout::getMvcInstance()->getView()->headScript()->appendFile(ZFDULCET_LIB_DIR_PLUGINS . '/mkadmin.colorpicker.js');
				$this->_colorPickerInitied = true;
			}
		}

		return parent::addElement($element, $name, $options);
	}
    
    public function addErrors2(array $messages)
    {
        foreach ($messages as $elementName => $elementMessages) {
            if (($element = $this->getElement($elementName)) instanceof Zend_Form_Element) {
                $element->addErrors($elementMessages);
            }
        }
    }

	public function getTypeForm() {
		return $this->_typeForm;
	}

	public function setTypeForm($typeForm) {
		return $this->_typeForm = $typeForm;
	}

}